package com.easyfun.healthmagicbox.gallary;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import cn.kuaipan.android.openapi.AuthSession;
import cn.kuaipan.android.openapi.KuaipanAPI;
import cn.kuaipan.android.openapi.session.AccessTokenPair;
import cn.kuaipan.android.openapi.session.AppKeyPair;
import cn.kuaipan.android.sdk.oauth.Session.Root;

public class KuaiPanTransporter {
	
    /**
     * Be sure to configure the consumer key and secret
     */
    private static final String APP_KEY = "" + "xc8vGOU1bMxGSdxF";
    private static final String APP_SECRET = "" + "YlrtbYeTLSrZJ8V8";

    final static private String ACCOUNT_PREFS_NAME = "prefs";
    final static private String ACCESS_KEY_NAME = "ACCESS_KEY";
    final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET";
    final static private String ACCESS_AUTH_TYPE_NAME = "ACCESS_AUTH_TYPE_NAME";
    final static private String ACCESS_UID_NAME = "ACCESS_UID_NAME";
	
	private static KuaiPanTransporter instance = null;
	Context context;
	KuaipanAPI mApi;
    private AuthSession mAuthSession;
    private int authType;
	public boolean mLoggedIn = false;
    
	public static KuaiPanTransporter getInstance(Context context)
	{
		synchronized (instance)
		{
			if (instance == null)
			{
				instance = new KuaiPanTransporter(context);
			}
			return instance;		
		}
	}
	private KuaiPanTransporter(Context context)
	{
        // We create a new AuthSession so that we can use the Kuaipan API.
        mAuthSession = buildSession();
        mApi = new KuaipanAPI(context, mAuthSession);
	}
	

    /**
     * Shows keeping the access keys returned from Trusted Authenticator in a
     * local store, rather than storing user name & password, and
     * re-authenticating each time (which is not to be done, ever).
     * 
     * @return Array of [access_key, access_secret, uid, auth_type], or null if
     *         none stored
     */
    private String[] getKeys() {
        SharedPreferences prefs = context.getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
        String key = prefs.getString(ACCESS_KEY_NAME, null);
        String secret = prefs.getString(ACCESS_SECRET_NAME, null);
        String uid = prefs.getString(ACCESS_UID_NAME, null);
        String authType = prefs.getString(ACCESS_AUTH_TYPE_NAME, null);
        if (key != null && secret != null) {
            String[] ret = new String[4];
            ret[0] = key;
            ret[1] = secret;
            ret[2] = uid;
            ret[3] = authType;
            return ret;
        } else {
            return null;
        }
    }

    /**
     * Shows keeping the access keys returned from Trusted Authenticator in a
     * local store, rather than storing user name & password, and
     * re-authenticating each time (which is not to be done, ever).
     */
    private void storeKeys(String key, String secret, String uid,
            String authType) {
        // Save the access key for later
        SharedPreferences prefs = context.getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
        Editor edit = prefs.edit();
        edit.putString(ACCESS_KEY_NAME, key);
        edit.putString(ACCESS_SECRET_NAME, secret);
        edit.putString(ACCESS_UID_NAME, uid);
        edit.putString(ACCESS_AUTH_TYPE_NAME, authType);
        edit.commit();
    }

    private void clearKeys() {
        SharedPreferences prefs = context.getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
        Editor edit = prefs.edit();
        edit.clear();
        edit.commit();
    }

    private AuthSession buildSession() {
        AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
        AuthSession session;

        String[] stored = getKeys();
        if (stored != null) {
            AccessTokenPair accessToken = new AccessTokenPair(stored[0],
                    stored[1]);
            if (TextUtils.isEmpty(stored[2])) {

            }
            authType = !TextUtils.isEmpty(stored[2]) ? Integer
                    .valueOf(stored[2]) : 0;
            session = new AuthSession(appKeyPair, accessToken, Root.KUAIPAN);
            this.authType = Integer.valueOf(stored[3]);
        } else {
            session = new AuthSession(appKeyPair, Root.KUAIPAN);
        }

        return session;
    }
    
    public void uploadFile()
    {
    	
    }
    
    class FileUploader implements Runnable
    {
    	String personID;
    	String filename;
    	FileUploader(String personID, String filename)
    	{
    		this.personID = personID;
    		this.filename = filename;
    	}
		@Override
		public void run() {
			if (!mLoggedIn ) {
	            // Start the authentication
	            mApi.startAuthForResult();	            
	        }
			
			
			
		}
    	
    }
    

    private void logOut() {
        // Remove credentials from the session
        // mAuthSession.unAuthorize();
        mApi.unAuthorize();
        // Clear our stored keys
        clearKeys();
        // Change UI state to display logged out version
        //setLoggedIn(false);
        mLoggedIn = false;
    }
}
